@page "/"
@using AM.Collections
@using AM.Web.Components
@inject BeriService _service

<PageTitle>Проект "Бери Читай"</PageTitle>

<div class="container">

    <div class="row my-5">
        <div class="col col-lg-6 offset-lg-3">
            <div class="input-group">
                <label for="keyword" class="form-label visually-hidden"></label>
                <input type="text" id="keyword" class="form-control"
                       placeholder="Ключевое слово"
                       @bind="_keyword"/>
                <button class="btn btn-primary form-control" @onclick="FindBooks">Поиск</button>
            </div>
        </div>
    </div>

    <div class="row my-5">
        <div class="col col-lg-6 offset-lg-3">
            <EditForm EditContext="_editContext" OnInvalidSubmit="OrderBooks">
                <div class="form-control-plaintext">Отобрано книг:
                    <strong>@CountSelected()</strong></div>
                <ValidationMessage For="() => _logon!.Ticket"/>
                <ValidationMessage For="() => _logon!.Email"/>
                <div class="input-group">
                    <label for="ticket" class="form-label visually-hidden"></label>
                    <InputText id="ticket" class="form-control" @bind-Value="_logon!.Ticket"></InputText>
                    <label for="email" class="form-label visually-hidden"></label>
                    <InputText id="email" class="form-control" @bind-Value="_logon!.Email"></InputText>
                    <button type="submit" class="btn btn-success form-control">Заказать</button>
                </div>
            </EditForm>
        </div>
    </div>

    <ErrorMessage @ref="_errorMessage"/>

    <BusyIndicator @ref="_busyIndicator" ImageSource="img/arctic-fox.gif" AltText="Подождите немного"/>

    <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4 row-cols-xl-5 g-4">

        @foreach (var book in _list)
        {
            <BeriBook Book="book"/>
        }

    </div>

    <div class="row m-5">
        <div class="col text-center"></div>
        <button class="btn btn-primary" @onclick="RandomBooks">Показать другие случайные книги</button>
    </div>

</div>

@code
{
    private EditContext? _editContext;
    private ValidationMessageStore? _messageStore;
    private bool _valudationSuccess;

    private List<BookInfo> _list { get; set; } = new();
    private string? _keyword;
    BusyIndicator? _busyIndicator;
    ErrorMessage? _errorMessage;
    private LogonModel? _logon;

    private int CountSelected()
    {
        return _list.Count (item => item.Selected);
    }

    private void OrderBooks()
    {
        if (_valudationSuccess)
        {
            try
            {
                _busyIndicator?.SetState (true);

                if (_service.Connect())
                {
                    var ticket = _logon!.Ticket;
                    var email = _logon!.Email;
                    var mfns = _list.Select (item => item.Mfn).ToArray();
                    var result = _service.OrderBooks (ticket, email, mfns);
                    if (!result.Ok)
                    {
                        _errorMessage?.SetMessage (result.Message);
                    }
                }
                else
                {
                    _errorMessage?.SetMessage ("Ошибка связи с сервером");
                }
            }
            finally
            {
                _service.Disconnect();
                _busyIndicator?.SetState (false);
            }

            // очищаем список книг
            _list = new();
        }
    }

    private void FindBooks()
    {
        _errorMessage?.Hide();
        _list = new();

        var keyword = _keyword;
        if (string.IsNullOrWhiteSpace (keyword))
        {
            return;
        }

        try
        {
            _busyIndicator?.SetState (true);

            if (_service.Connect())
            {
                _list = _service.SearchBooks (keyword).ToList();
            }
            else
            {
                _list = new();
            }
        }
        finally
        {
            _service.Disconnect();
            _busyIndicator?.SetState (false);
        }

        if (_list.IsNullOrEmpty())
        {
            _errorMessage?.SetMessage ("Ничего не найдено");
        }
    }

    private void RandomBooks()
    {
        _errorMessage?.Hide();

        try
        {
            _busyIndicator?.SetState (true);

            if (_service.Connect())
            {
                _list = _service.RandomBooks().ToList();
            }
            else
            {
                _list = new();
            }
        }
        finally
        {
            _service.Disconnect();
            _busyIndicator?.SetState (false);
        }
    }

    private void HandleValidationRequested
        (
            object? sender,
            ValidationRequestedEventArgs args
        )
    {
        _valudationSuccess = true;

        if (_messageStore is {} store)
        {
            store.Clear();
            var earlyReturn = false;
            var countSelected = CountSelected();

            if (countSelected == 0)
            {
                store.Add (() => _logon!.Ticket, "Не выбраны книги");
            }

            if (string.IsNullOrWhiteSpace (_logon!.Ticket))
            {
                store.Add (() => _logon.Ticket, "Не указан читательский!");
                earlyReturn = true;
            }

            if (string.IsNullOrWhiteSpace (_logon.Email))
            {
                store.Add (() => _logon.Email, "Не указан e-mail!");
                earlyReturn = true;
            }

            if (earlyReturn)
            {
                _valudationSuccess = false;
                return;
            }

            try
            {
                _busyIndicator?.SetState (true);

                if (_service.Connect())
                {
                    var reader = _service.FindReader (_logon.Ticket, _logon.Email);
                    if (reader is null)
                    {
                        _valudationSuccess = false;
                        store.Add (() => _logon.Ticket, "Неверные данные");
                    }
                }
                else
                {
                    _valudationSuccess = false;
                    store.Add (() => _logon.Ticket, "Ошибка связи с сервером");
                }
            }
            finally
            {
                _service.Disconnect();
                _busyIndicator?.SetState (false);
            }

        }
    }

    protected override void OnInitialized()
    {
        _logon = new LogonModel();
        _editContext = new EditContext (_logon);
        _editContext.OnValidationRequested += HandleValidationRequested;
        _messageStore = new ValidationMessageStore (_editContext);
        RandomBooks();
    }
}
